package com.whwj.heaven.auth.handler;

import cn.hutool.json.JSONUtil;
import com.wjwh.heaven.common.core.utils.Result;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author dm
 * @Date: 2021/2/25 11:07
 * @Description:
 */
@Component
public class HeavenAuthenticationFailureHandler  implements AuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException {
        System.out.println("--------------------");
        String message = e.getMessage();
        String body= JSONUtil.toJsonStr(Result.failure(HttpStatus.UNAUTHORIZED.value(),message));
        response.getWriter().write(body);
    }
}
